package com.wanghai.carwashing.api.controller.v1;

import com.wanghai.carwashing.api.controller.BaseController;
import com.wanghai.carwashing.api.exception.ApiException;
import com.wanghai.carwashing.api.interceptor.ApiCallLogInterceptor;
import com.wanghai.carwashing.common.helper.JsonHelper;
import com.wanghai.carwashing.common.helper.SmsHelper;
import com.wanghai.carwashing.common.model.vo.SmsValidateCode;
import com.wanghai.carwashing.common.util.DateUtil;
import com.wanghai.carwashing.common.util.SecurityCode;
import com.wanghai.carwashing.common.util.StringUtil;
import com.wanghai.carwashing.core.bean.APICallLog;
import com.wanghai.carwashing.core.form.SmsCodeForm;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 短信验证码SmsValidateCodeController
 * @author 王海
 */
@Controller
@RequestMapping("/v1/sms")
public class SmsValidateCodeController extends  BaseController{
    /**
     * 获取短信验证码
     * @return 
     */
    @RequestMapping(value = "/code",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseEntity<String> getValidateCode(@Valid @ModelAttribute SmsCodeForm codeForm,BindingResult bindingResult) throws ApiException{
        if(bindingResult.hasErrors()){
            List<ObjectError> errors = bindingResult.getAllErrors();
            List<String> errMsgs = new ArrayList<String>(errors.size());
            for (ObjectError error : errors) {
              errMsgs.add(error.getDefaultMessage());
            }
            APICallLog callLog = (APICallLog) request.getAttribute(ApiCallLogInterceptor.KEY_APICALL_LOG);
            String err = StringUtil.join(errMsgs, ",");
            callLog.setErrorCode(1003);
            callLog.setStatusCode(400);
            callLog.setDescb(err);
            request.setAttribute(ApiCallLogInterceptor.KEY_APICALL_LOG, callLog);
            throw  new ApiException(1003, err, 400);
        }
        try {
            SmsValidateCode code = new SmsValidateCode();
            code.setPhone(codeForm.getPhone());
            code.setCode(SecurityCode.getSecurityCode());
            code.setCreateTime(DateUtil.format(new Date()));
            code.setStartTimestamp(System.currentTimeMillis());
            code.setEndTimestamp(System.currentTimeMillis()+1000*60*10);
            System.out.println("json:"+JsonHelper.buildJsonString(code));
            APICallLog callLog = (APICallLog) request.getAttribute(ApiCallLogInterceptor.KEY_APICALL_LOG);
            callLog.setErrorCode(0);
            callLog.setStatusCode(HttpStatus.OK.value());
            callLog.setDescb("ok");
            request.setAttribute(ApiCallLogInterceptor.KEY_APICALL_LOG, callLog);
            return new ResponseEntity<String>(JsonHelper.buildJsonString(SmsHelper.sendSmsValidateCode(code)), JsonHelper.buildJsonHeaders(), HttpStatus.OK);
        } catch (Exception ex) {
            Logger.getLogger(SmsValidateCodeController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
